package eu.connect.im;

import java.io.IOException;
import java.net.Socket;

import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.impl.DefaultHttpClientConnection;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.params.SyncBasicHttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpProcessor;
import org.apache.http.protocol.HttpRequestExecutor;
import org.apache.http.protocol.ImmutableHttpProcessor;
import org.apache.http.protocol.RequestConnControl;
import org.apache.http.protocol.RequestContent;
import org.apache.http.protocol.RequestExpectContinue;
import org.apache.http.protocol.RequestTargetHost;
import org.apache.http.protocol.RequestUserAgent;

public class HTTPServerProxyThread implements Partner
{
	private HttpHost target;
	private MessageQueue queue;
	private Partner correpondingPartner;
	
	public HTTPServerProxyThread(String host, int port, MessageQueue queue)
	{
		this.target = new HttpHost(host, port);
		this.queue = queue;
	}	
	
	@Override
	public void sendMessage(Message msg)
	{
		HttpParams params = new SyncBasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, "UTF-8");
        HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
        HttpProtocolParams.setUseExpectContinue(params, true);
        
        Socket outsocket = null;
		try
		{
			outsocket = new Socket(target.getHostName(), target.getPort());
			
	        DefaultHttpClientConnection outconn = new DefaultHttpClientConnection();
			outconn.bind(outsocket, params);
			
			HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] {
					new RequestContent(),
					new RequestTargetHost(),
					new RequestConnControl(),
					new RequestUserAgent(),
					new RequestExpectContinue()});
			
			HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
			
			HttpContext context = new BasicHttpContext(null);
			context.setAttribute(ExecutionContext.HTTP_CONNECTION, outconn);
	        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target);
			
	        HttpRequest request = (HttpRequest)msg.getMessageEntity();
	                    
			httpexecutor.preProcess(request,httpproc, context);
			HttpResponse targetResponse = httpexecutor.execute(request, outconn, context);
	        httpexecutor.postProcess(targetResponse, httpproc, context);
	        
	        targetResponse.removeHeaders(HTTP.CONTENT_LEN);
            targetResponse.removeHeaders(HTTP.TRANSFER_ENCODING);
            targetResponse.removeHeaders(HTTP.CONN_DIRECTIVE);
            targetResponse.removeHeaders("Keep-Alive");
            targetResponse.removeHeaders("TE");
            targetResponse.removeHeaders("Trailers");
            targetResponse.removeHeaders("Upgrade");
            
            Message response = new Message(targetResponse, "HttpResponse", "server");
            response.setMyrole(this);
            response.setYourrole(this.correpondingPartner);
            this.queue.addMessage(response);
	        
	        
		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void setCorrepondingPartner(Partner correpondingPartner)
	{
		this.correpondingPartner = correpondingPartner;
	}

}
